library(tidyplots)

study |>
  ggplot(aes(treatment, score)) +
  geom_point()

# publication: small font size? ------
# 50x50 inch, really publicable.
study |> 
  tidyplot(x = treatment, y = score) |> 
  add_data_points()

# connect 'layer' by pipe instead of + ----------
study |> 
  tidyplot(x = treatment, y = score) |> 
  add_data_points() |>
  add_mean_bar(alpha = .4)

study |>
  tidyplot(x = treatment, y = score, color = dose) |> 
  add_data_points() |>
  add_mean_dash() |>
  add_sem_errorbar()

# add_xx / remove_xx ---------
study |> 
  tidyplot(x = group, y = score, color = dose) |> 
  add_data_points() |> 
  add_mean_dash() |> 
  add_sem_errorbar() |> 
  remove_legend_title() |> 
  remove_y_axis()

# adjust width, height ---------
# beeswarm from ggbeeswarm, open scatters allow overlaps
study |> 
  tidyplot(x = treatment, y = score, color = treatment) |> 
  add_data_points_beeswarm(shape = 1) |>
  add_mean_bar(alpha = 0.4) |> 
  add_sem_errorbar() |> 
  adjust_size(width = 20, height = 20)

# why not labs()?
study |> 
  tidyplot(x = treatment, y = score, color = treatment) |> 
  add_data_points() |> 
  add_mean_bar(alpha = 0.4) |> 
  add_sem_errorbar() |> 
  adjust_title("This is my fantastic plot title") |>
  adjust_x_axis_title("Treatment group") |>
  adjust_y_axis_title("Disease score") |>
  adjust_legend_title("") |>
  adjust_caption("Here goes the caption")

# oh, color & fill is not the same?
study |> 
  tidyplot(x = treatment, y = score, color = treatment) |> 
  add_data_points() |> 
  add_mean_bar(alpha = 0.4) |> 
  add_sem_errorbar() +
  labs(title = "This is my fantastic plot title",
       x = "Treatment group", y = "Disease score")

# adjust color & fill in one ---------
study |> 
  tidyplot(x = treatment, y = score, color = treatment) |> 
  add_data_points() |> 
  add_mean_bar(alpha = 0.4) |> 
  add_sem_errorbar() |> 
  adjust_colors(new_colors = c("#644296","#F08533","#3B78B0", "#D1352C"))

## pick built-in palettes by color_xx --------
colors_discrete_friendly_long
colors_continuous_viridis
colors_diverging_blue2red
colors_diverging_BuYlRd

## more color will be auto-interpolated
energy |> 
  tidyplot(year, energy, color = energy_source) |> 
  add_barstack_absolute()

## make your own palette by hex
tpreds <- new_color_scheme(c('#fff', '#f00'))

tpreds

# reorder make easy! --------
study |> 
  tidyplot(x = treatment, y = score, color = treatment) |> 
  add_data_points() |> 
  add_mean_bar(alpha = 0.4) |> 
  add_sem_errorbar() |> 
  reorder_x_axis_labels("D", "C")

study |> 
  tidyplot(x = treatment, y = score, color = treatment) |> 
  add_data_points() |> 
  add_mean_bar(alpha = 0.4) |> 
  add_sem_errorbar() |> 
  sort_x_axis_labels("D", "C")

# split, but not by "facets" -----
study |> 
  tidyplot(x = group, y = score, color = group) |> 
  add_data_points() |> 
  add_sem_errorbar() |> 
  add_mean_dash() |> 
  split_plot(by = dose)

# save plot while view it ---------
study |> 
  tidyplot(x = group, y = score, color = group) |> 
  add_data_points() |> 
  add_sem_errorbar() |> 
  add_mean_dash() |> 
  save_plot("my_plot.pdf")

# heatmap! ------
gene_expression |> 
  tidyplot(x = sample, y = external_gene_name, color = expression) |> 
  add_heatmap(scale = 'row')

# stat test ----------
study |> 
  tidyplot(x = dose, y = score, color = group) |> 
  add_mean_dash() |> 
  add_sem_errorbar() |> 
  add_data_points() |> 
  add_test_asterisks()

study |> 
  tidyplot(x = treatment, y = score, color = treatment) |> 
  add_mean_dash() |> 
  add_sem_errorbar() |> 
  add_data_points() |> 
  add_test_pvalue(ref.group = "A", hide_info = T)

# highlight points ------
animals |> 
  tidyplot(x = weight, y = speed) |> 
  add_data_points() |> 
  add_data_points(data = max_rows(weight, 1), color = "red", shape = 1, size = 2) |> 
  add_data_points(data = max_rows(speed, 1), color = "red", shape = 1, size = 2)

animals |> 
  tidyplot(x = weight, y = speed) |>
  add_reference_lines(x = 4000, y = c(100, 200)) |>
  add_data_points() |> 
  add_data_labels_repel(data = max_rows(weight, 3), animal) |> 
  add_data_labels_repel(data = max_rows(speed, 3), animal)
